<?php
/**
 * @name          wx-qq-function    
 * @copyright     http://code.google.com/u/wxframework/
 * @version       $Id: upload.class.php 26 2009-04-15 01:19:51Z sean.fmais $
 * @author        $Author: sean.fmais $  <fmais@163.com>
 * 
**/
 class cls_upfile{
       
      var  $upfile_dir = UP_PATH;
	  var  $allowmedia = array('image/pjpeg','image/gif');
	  var  $err = array('size'=>'file size exceed','type'=>'file type error','copy'=>'file copy error');
	  var  $maxsize = 100000;
	  var  $rate = 75;
	  var  $markwords = null;
	  var  $markimage = null;
	  var  $debug = false;
/* 	  php 5 
     function __construct($upfile_dir="",$allowmedia='',$err='',$maxsize=100000){
	    if (!empty($upfile_dir)){ $this->upfile_dir = $upfile_dir;}
		if (!is_array($allowmedia) || empty($allowmedia) ){ $allowmedia = array('image/pjpeg','image/gif');}
        if (!is_array($err) || empty($err) ){ $err = array('size'=>'file size exceed','type'=>'file type error','copy'=>'file copy error');}
	    $this->allowmedia=$allowmedia;
	    $this->err=$err;
	    $this->maxsize=$maxsize;
	  } 
*/	      
/* 	  php 4 */
 function cls_upfile($upfile_dir="",$allowmedia='',$err='',$maxsize=100000){
	if (!empty($upfile_dir)){ $this->upfile_dir = $upfile_dir;}
	if (!is_array($allowmedia) || empty($allowmedia) ){ $allowmedia = array('image/pjpeg','image/gif');}
	if (!is_array($err) || empty($err) ){ $err = array('size'=>'file size exceed','type'=>'file type error','copy'=>'file copy error');}
	$this->allowmedia=$allowmedia;
	$this->err=$err;
	$this->maxsize=$maxsize;
  }    
  
	   
//$needAll=0只需要缩略图1需要缩略图，原图，2只需要原图
 function upmanyfiles($fileArr,$fileTxtArr,$id,$picw,$pich,$needAll="1"){
	       $allowmedia = $this->allowmedia;
		   $err = $this->err;
		   $maxsize = $this->maxsize;
		   $path = $this->upfile_dir;
		   $rate = $this->rate;
		   $markwords = $this->markwords;
		   $markimage = $this->markimage;
		   
          $fileid=floor($id/2000);
		  @mkdir($path.$fileid."/",0777);
		  $dirname=$path.$fileid."/".$id;
          @mkdir($dirname,0777);
       if(count($fileArr)!=0){ 	
	                
          while(list($fileAtrribName,$fileAtrribValue)=each($fileArr)){
		       // echo "$fileAtrribName=>$fileAtrribValue"; 		   
		    	$i=0;	
		     	while(list($valueId,$value)=each($fileAtrribValue)){ 			  
		        	     $upfile[$i][$fileAtrribName]=$value;				
		       	         $i++;
			          }
		   } 	
		   
		   
           while(list($fileId,$file)=each($upfile)){
		         //echo "<font color=red>$fileId,$file </font><br>";
		       $picid=mt_rand(10000,20000)+$fileId;
               if($file['tmp_name']!=''){
			       if($needAll=="1"){  			 
		                 $upResult=$this->copyfile($file,$dirname."/",$maxsize,$allowmedia,$err,'',$fileId);
						 // echo "s=".$upResult['success'];
						 // echo "m=".$upResult['mesg'];
		          		if($upResult['success']==1){
								//  echo $dirname."/".$upResult['name']."<br>";	
								//  echo $dirname."/".$picid.".jpg<br>";   
								$this->makethumb($dirname."/".$upResult['name'],$dirname."/".$picid.".jpg",$picw,$pich,$rate,$markwords,$markimage);
								if($fileId==0){ $picname[0]=$picid.".jpg"; }
								$up['mesg']=$upResult['mesg'];
								$allname.=$upResult['name']."|".$picid.".jpg"."|";
								$pictext.=$fileTxtArr[$fileId]."|";
								
		    		   }else{
					        if(defined('SYS_DEBUG') && SYS_DEBUG) {
								 alert($upResult['mesg'],'back',1);
							}else{
								$up['mesg']=$upResult['mesg'];
							}
					   }
					   
		          }elseif($needAll=="2"){
				  
		    		    $upResult=$this->copyfile($file,$dirname."/",$maxsize,$allowmedia,$err,"",$fileId);
			 			if($upResult['success']==1){
			 				     if($fileId==0){$picname[0]= $upResult['name'];}
								 $allname.= $upResult['name']."|";
								 $pictext.=$fileTxtArr[$fileId]."|";
								 $up['mesg']=$upResult['mesg'];
						}else{
					        if(defined('SYS_DEBUG') && SYS_DEBUG) {
								 alert($upResult['mesg'],'back',1);
							}else{
								$up['mesg']=$upResult['mesg'];
							}
						}
							 
		         }elseif($needAll=="0"){
		                $this->makethumb($file['tmp_name'],$dirname."/".$picid.".jpg",$picw,$pich,$rate,$markwords,$markimage);
						if($fileId==0){$picname[0]=$picid.".jpg";}
		   				$allname.= "*"."|".$picid.".jpg|";
		   				$pictext.=$fileTxtArr[$fileId]."|";
		   		 }	// end $needAll	   
	    
              } // end $file['tmp_name']
	       }// end while  
		  //echo "allname=".$allname;
		   $up['0']=$picname[0];
		   $up['pictext']=$pictext;
		   $up['allname']=$allname; 
	     } 
		  ($this->debug) && print_r($up);
		  return $up;
}




	
/**
* 保存文件
* @param string  $file 	  	  - FILE变量
* @param string $path      	  - 保存路径
* @param integer $maxsize        - 最大值
* @param string $allowmedia    - 允许格式
* @param string $err    		- 错误类型
* @param string $newname    	- 新名称，留空则随机
* @param string $arrnum    		- 批量上传文件编号ID
* @return (bool) 
*/
function copyfile($file,$path,$maxsize=100000,$allowmedia,$err,$newname,$arrnum){
                   
				   if ($file['size']>$maxsize){				  
				      ($this->debug) && $mesg.="<hr>".$file['name']."......".$err['size'].$maxsize."<".$file['size']."<br>";				                     //echo "m1=".$mesg;
				       $mesg = $err['size'];
				    }
					
				   $i=$allowcount=0;	
				   				 			   
				   while($allowmedia[$i]!=''){
                      if ($file['type']==$allowmedia[$i]){$allowcount++;}
				      $i++;
				    }
					
				   if($allowcount==0){			 		  
				       ($this->debug) &&  $mesg.="<hr>".$file['name']."...".$file['type']."...".$err['type']."<br>";
						$mesg = $err['type'];		 			  				  
				    }
					
				  if ($newname==""){
				        $f=explode(".",$file['name']);
						mt_srand(time());
						$picname=mt_rand(10000,99999).$arrnum.".".$f[count($f)-1]; 
						$picname=str_replace(";","",$picname);
						$picname=str_replace("|","",$picname);
						$picname=str_replace(" ","",$picname);
						$filename=$path.$picname;
				 }else{	
					  				
						$filename=$path.$newname;					
				 }
					//echo "f=".$filename;
					
					if( $file['size']<$maxsize && $allowcount>0){
					       
						   if (!copy($file['tmp_name'],$filename)){
								($this->debug) &&  $mesg.="<hr>".$file['name']."......".$err['copy']."<br>";
								$mesg = $err['copy'];
								$updata['success']=0;
								$updata['mesg']=$mesg;
								return $updata;
								}else{ 
								$updata['success']=1;
								$updata['name']=$picname;
								return $updata;
							   }
					 }else{				 
					            $updata['success']=0;
								$updata['mesg']=$mesg;
								return $updata;	
					 }	
					  
}
		  



/**
* 缩放和水印
* @param string  $srcFile 	  - 来源文件
* @param string $dstFile      - 输出文件
* @param integer $dstW        - 缩放宽度
* @param integer $dstH        - 缩放高度
* @param integer  $rate       - 图片质量(0-100) 默认75
* @param string $markwords    - 水印文字(不建议使用中文)
* @param string $markimage    - 水印图片(100*35 px)
* @return (bool) 
*/
function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=75,$markwords=null,$markimage=null){
		$data = getimagesize($srcFile);	
		switch($data[2])
		{
		  case 1:
			   $im=@imagecreatefromgif($srcFile);
			   break;
		  case 2:
			   $im=@imagecreatefromjpeg($srcFile);
			   break;
		  case 3:
			   $im=@imagecreatefrompng($srcFile);
			   break;
		}
		if(!$im) return False;
		
		$srcW=imagesx($im);
		$srcH=imagesy($im);
		$dstX=0;
		$dstY=0;
		if ($srcW/$dstW>$srcH/$dstH)
		{
			$fdstH = round($srcH*$dstW/$srcW);
			$dstY  = floor(($dstH-$fdstH)/2);
			$fdstW = $dstW;
			
		}else{
		
			$fdstW = round($srcW*$dstH/$srcH);
			$dstX  = floor(($dstW-$fdstW)/2);
			$fdstH = $dstH;
			
		 }
		 
		$ni=imagecreatetruecolor($dstW,$dstH);
		//$dstX=($dstX<0)?0:$dstX;
		//$dstY=($dstX<0)?0:$dstY;
		$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
		$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
		$white = imagecolorallocate($ni,255,255,255); // ImageColorAllocate($ni,R,G,B)
		imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);
		imagecopyresized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);

		if($markwords!=null){
			imagestring($ni,2,3,3,$markwords,$white);
		}elseif($markimage!=null){
			$wimage =imagecreatefromgif($markimage);
			imagecopy($ni,$wimage,0,0,0,0,100,35);
			imagedestroy($wimage);
		}
		
		imagejpeg($ni,$dstFile,$rate);
		imagedestroy($im);
		imagedestroy($ni);
		
}
	

		  
		
}
?>